/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.biz.master.ui;

import app.utils.SystemUtil;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.biz.invoicesystem.master.ui.CustomerMasterUI2;
import org.biz.invoicesystem.ui.list.master.CustomerListUi;
import org.biz.invoicesystem.ui.list.master.ItemList;
import org.components.util.Sessions;
import org.components.windows.TabPanelUI;

/**
 *
 * @author d
 */
public class MasterDetailViewerUI extends javax.swing.JPanel {

    /**
     * Creates new form MasterDetailViewerUI
     */
    public MasterDetailViewerUI() {
        initComponents();

    }

    public void init(TabPanelUI tab,String detailMas, TabPanelUI listViewUI) {
        jTabbedPane1.add(tab, detailMas);
        tab.setTabName(detailMas+"DETAIL");
        jTabbedPane1.add(listViewUI, detailMas);
        listViewUI.setTabName(detailMas+"LIST");
        
        SystemUtil.addTabToSessions(detailMas+"DETAIL", tab);
        SystemUtil.addTabToSessions(detailMas+"LIST", listViewUI);
        jTabbedPane1.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                ((TabPanelUI) jTabbedPane1.getSelectedComponent()).updateEntityUI();
            }
        });

    }
    
    public static void addTabPane(JTabbedPane tab, TabPanelUI panelUI,String tabName ){
    
        try {
            TabPanelUI tpui = (TabPanelUI) Sessions.getObj(tabName);
            int ix = tab.indexOfTab(tabName);
            if (tpui == null) {
//                tpui = (TabPanelUI) Class.forName(cls.getName()).newInstance();
                tab.add(tabName, tpui);
                int sx = tab.indexOfTab(tabName);
                tab.setSelectedIndex(sx);
                Sessions.addToSession(tabName, tpui);

            } else {
                tab.setSelectedIndex(ix);
//                tpui.setobj(obj);
            }
        
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    //<editor-fold defaultstate="collapsed" desc=" main method Look and feel setting code (optional) ">
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
        }

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                MasterDetailViewerUI it = new MasterDetailViewerUI();
                SystemUtil.addTabToSessions("main", it);
                ItemMasterUI2 item = new ItemMasterUI2();
                ItemList list = new ItemList();

                CustomerMasterUI2 customerMasterUI2 = new CustomerMasterUI2();
                CustomerListUi custList = new CustomerListUi();
                it.init(item,"Item", list);
                it.init(customerMasterUI2,"Customer", custList);
                JFrame fram = new JFrame();
                fram.getContentPane().add(it);
                fram.setSize(700, 700);
                fram.setVisible(true);
            }
        });
    }
    //</editor-fold>

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTabbedPane1 = new javax.swing.JTabbedPane();

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 278, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTabbedPane jTabbedPane1;
    // End of variables declaration//GEN-END:variables
}
